package com.mk.legoscans2;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ScanListAdapter extends ArrayAdapter<Scan>{

	private List<Scan> _scans;
	private LayoutInflater _inflater;
	private Activity _activity;
	private String _account;
	
	public ScanListAdapter(Activity activity, List<Scan> scans, String account) {
		super(activity, R.layout.scan_list_item, scans);
		
		_inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		_scans = scans;
		_activity = activity;
		_account = account;
	}
	
	public static class ViewHolder{
		public TextView scanHeader;
		public TextView scanInfo1;
		public TextView scanInfo2;
		public ImageView scanThumbnail;
		public ImageView starImage;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v = convertView;
		
		ViewHolder holder;
		
		if(v == null)
		{
			v = _inflater.inflate(R.layout.scan_list_item, null);
			holder = new ViewHolder();
			holder.scanHeader = (TextView)v.findViewById(R.id.scan_header);
			holder.scanInfo1 = (TextView)v.findViewById(R.id.scan_info_1);
			holder.scanInfo2 = (TextView)v.findViewById(R.id.scan_info_2);
			holder.scanThumbnail = (ImageView)v.findViewById(R.id.scan_thumbnail);
			holder.starImage = (ImageView)v.findViewById(R.id.star_image);
			v.setTag(holder);
		}
		else
			holder = (ViewHolder)v.getTag();
		
		final Scan scan = _scans.get(position);
		if(scan != null)
		{
			holder.scanHeader.setText("#" + scan._boxNumber + " " + scan._name);
			if(scan._year != null)
				holder.scanInfo1.setText("Year: " + scan._year);
			else
			{
				holder.scanInfo1.setVisibility(View.INVISIBLE);
				holder.scanInfo1.setHeight(0);
			}
			holder.scanInfo2.setText("Pages: " + scan._pageCount);
			holder.scanThumbnail.setTag(scan._scanId);
			
			SetStarImageTask setStarImageTask = new SetStarImageTask(_account, scan._scanId, holder.starImage);
			setStarImageTask.execute();
			
			LegoScans3Application.ImageManager.displayImage(scan._scanId, _activity, holder.scanThumbnail);
		}
		
		return v;
	}
	
	private class SetStarImageTask extends AsyncTask<String, Void, Boolean> {

		String _selectedAccount;
		String _scanId;
		ImageView _starImage;
		
		public SetStarImageTask(String selectedAccount, String scanId, ImageView starImage){
			_selectedAccount = selectedAccount;
			_scanId = scanId;
			_starImage = starImage;
		}
		
		protected Boolean doInBackground(String... params) {
			return LegoScans3Application.FavoritesService.isFavorite(_selectedAccount, _scanId);
		}
		
		protected void onPostExecute(Boolean result) {

			_starImage.setImageResource(result ? R.drawable.btn_rating_star_off_pressed : R.drawable.btn_rating_star_off_normal);
			_starImage.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					new ToggleFavoriteTask(_starImage, _scanId).execute();
				}
			});
		}
	}
	private class ToggleFavoriteTask extends AsyncTask<String, Void, Boolean> {

		ImageView _starImage;
		String _scanId;
		
		public ToggleFavoriteTask(ImageView starImage, String scanId) {
			_starImage = starImage;
			_scanId = scanId;
		}

		@Override
		protected Boolean doInBackground(String... arg0) {
			return LegoScans3Application.FavoritesService.toggleFavorite(_account, _scanId);
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			_starImage.setImageResource(result ? R.drawable.btn_rating_star_off_pressed : R.drawable.btn_rating_star_off_normal);
		}
	}
}
